package com.baizhi.action;


import com.baizhi.dao.UserDAO;
import com.baizhi.dao.UserDAOImpl;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class UserLoginAction extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String account = req.getParameter("account");
        req.getSession().setAttribute("account",account);
        String password = req.getParameter("password");
        String code = req.getParameter("code");

        UserService us = new UserServiceImpl();
        User login = us.login(account,password);

        HttpSession session = req.getSession();
        String validationCode = (String)session.getAttribute("validationCode");

        UserService usc = new UserServiceImpl();
        User user1 = new User();
        user1.setAccount(account);

        User user = usc.show(user1);
        session.setAttribute("u",user);
        System.out.println("后台"+user);



        System.out.println("yz"+validationCode);
        System.out.println("code"+code);
        if(validationCode.equals(code)){
            if(login != null){
                session.setAttribute("flag","y");
                session.setAttribute("login",account);
                req.getRequestDispatcher("/index.jsp").forward(req,resp);
            }else{
                req.setAttribute("logErrors","账号密码错误");
                resp.sendRedirect(req.getContextPath()+"/login.jsp");
            }
        }else {
            req.setAttribute("logError","验证码错误");
            resp.sendRedirect(req.getContextPath()+"/login.jsp");
        }
    }
}
